﻿using Net.Chirash.Twitter.TwitterDatabase;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using Net.Chirash.Twitter.TwitterClient;

namespace TwitterDatabaseTest
{


    /// <summary>
    ///TwitterCachingTest のテスト クラスです。すべての
    ///TwitterCachingTest 単体テストをここに含めます
    ///</summary>
    [TestClass()]
    public class TwitterCachingTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///現在のテストの実行についての情報および機能を
        ///提供するテスト コンテキストを取得または設定します。
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region 追加のテスト属性
        // 
        //テストを作成するときに、次の追加属性を使用することができます:
        //
        //クラスの最初のテストを実行する前にコードを実行するには、ClassInitialize を使用
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //クラスのすべてのテストを実行した後にコードを実行するには、ClassCleanup を使用
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //各テストを実行する前にコードを実行するには、TestInitialize を使用
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //各テストを実行した後にコードを実行するには、TestCleanup を使用
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        public static TwitterCachingConnection testConnection = new TwitterCachingConnection(@"Data Source=|DataDirectory|\TwitterModel.sdf");
        public static TwitterCommand command = new TwitterCommand()
        {
            Connection = new TwitterConnection()
            {
                ConsumerKey = "8UwTJZJMJjl4uLZduYvA",
                ConsumerSecret = "eCnmRK0mDSXfjm95j947iixyDaUY3OZleSdkWMhLc",
                Token = "141500207-0sEjrUZwRXujtATsDh4gFSAX3qhCVycqJBtDiWuq",
                TokenSecret = "jZZKmbXhJzh933Eg84P7bEIjNBbp3uNM3q52i8CwU",
            }
        };

        private User getCurrentUserByCommand()
        {
            if (command.CurrentUser == null)
            {
                command.VerifyCredentials();
                command.CurrentUser.Friends = new List<User>(command.Friends(command.CurrentUser));
                command.CurrentUser.Followers = new List<User>(command.Followers(command.CurrentUser));

                var caching = new TwitterCaching(testConnection);
                //caching.UpdateUser(new User[] { this.command.CurrentUser });
            }

            return command.CurrentUser;
        }

        private void Enumerate(IEnumerable<TwitterData> datas)
        {
            if (datas == null)
                return;
            foreach (var data in datas)
                Console.WriteLine(data.ToString());
        }


        /// <summary>
        ///TwitterCaching コンストラクター のテスト
        ///</summary>
        [TestMethod()]
        public void TwitterCachingConstructorTest()
        {
            TwitterCachingConnection conn = testConnection;
            TwitterCaching target = new TwitterCaching(conn);
        }

        /// <summary>
        ///UpdateStatus のテスト
        ///</summary>
        [TestMethod()]
        public void UpdateStatusTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            IEnumerable<Status> stas = command.GetHomeTimeline();
            //this.updatedStatuses = stas;
            target.UpdateStatuses(stas);
        }
        // private IEnumerable<Status> updatedStatuses;

        /// <summary>
        ///GetAllStatus のテスト
        ///</summary>
        [TestMethod()]
        public void GetAllStatusTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            IEnumerable<Status> actual;
            actual = target.GetAllStatus();
        }


        /// <summary>
        ///UpdateUser のテスト
        ///</summary>
        [TestMethod()]
        public void UpdateUserTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            User user = command.VerifyCredentials();
            IEnumerable<User> users = command.Friends(user);
            target.UpdateUsers(users);
            IEnumerable<User> followers = command.Followers(user);
            target.UpdateUsers(followers);
        }


        /// <summary>
        ///CreateHashTag のテスト
        ///</summary>
        [TestMethod()]
        public void CreateHashTagTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            ICollection<HashTag> actual;
            actual = target.CreateHashTag(command.GetHomeTimeline(new TimelineOption() { Count = 1 }).First());
        }


        /// <summary>
        ///UpdateUserFriends のテスト
        ///</summary>
        [TestMethod()]
        public void UpdateUserFriendsTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            long userid = this.getCurrentUserByCommand().UserID;
            IEnumerable<User> friends = command.Friends(this.getCurrentUserByCommand());
            target.UpdateUsers(new User[]{this.getCurrentUserByCommand()});
            target.UpdateUsers(friends);

            target.UpdateUserFriends(userid, friends);
            target.UpdateUserFriends(userid, friends);
        }

        /// <summary>
        ///ExistDirectMessage のテスト
        ///</summary>
        [TestMethod()]
        public void ExistDirectMessageTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            long directmessageId = 0;
            bool actual;
            actual = target.ExistDirectMessage(directmessageId);
        }

        /// <summary>
        ///ExistStatus のテスト
        ///</summary>
        [TestMethod()]
        public void ExistStatusTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            Status sample = command.GetHomeTimeline().First();
            long statusId = sample.StatusID;
            target.UpdateStatuses(new Status[] { sample });

            bool actual;
            actual = target.ExistStatus(statusId);
            Assert.AreEqual(true, actual);
        }

        /// <summary>
        ///ExistUser のテスト
        ///</summary>
        [TestMethod()]
        public void ExistUserTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            User sample = command.Friends(this.getCurrentUserByCommand()).First();
            long userid = sample.UserID;
            target.UpdateUsers(new User[] { sample });

            bool actual;
            actual = target.ExistUser(userid);
            Assert.AreEqual(true, actual);
        }

        /// <summary>
        ///ExistUserFollower のテスト
        ///</summary>
        [TestMethod()]
        public void ExistUserFollowerTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            User targetUser = getCurrentUserByCommand();
            target.UpdateUsers(new User[] { getCurrentUserByCommand() });

            var sample = command.Followers(this.getCurrentUserByCommand());
            target.UpdateUsers(sample);
            target.UpdateUserFollowers(command.CurrentUser.UserID, sample);

            User follower = new User() { UserID = 143026349 };
            bool expected = true;
            bool actual;
            actual = target.ExistUserFollower(targetUser, follower);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///ExistUserFriend のテスト
        ///</summary>
        [TestMethod()]
        public void ExistUserFriendTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            User targetUser = getCurrentUserByCommand();
            target.UpdateUsers(new User[]{getCurrentUserByCommand()});

            IEnumerable<User> sample = command.Friends(this.getCurrentUserByCommand());
            target.UpdateUsers(sample);
            target.UpdateUserFriends(targetUser.UserID,sample);
            Console.WriteLine("sample");
            Enumerate(sample);

            User friend = new User() { UserID = 143026349 };
            bool expected = true;
            bool actual;
            actual = target.ExistUserFriend(targetUser, friend);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///GetDirectMessage のテスト
        ///</summary>
        [TestMethod()]
        public void GetDirectMessageTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            long directmessageId = 0; // TODO: 適切な値に初期化してください
            DirectMessage expected = null; // TODO: 適切な値に初期化してください
            DirectMessage actual;
            actual = target.GetDirectMessage(directmessageId);
            Assert.AreEqual(expected, actual);
            Assert.Inconclusive("このテストメソッドの正確性を確認します。");
        }

        /// <summary>
        ///GetStatus のテスト
        ///</summary>
        [TestMethod()]
        public void GetStatusTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            Status sample = command.GetHomeTimeline().First();
            long statusId = sample.StatusID;
            target.UpdateStatuses(new Status[] { sample });

            Status actual;
            actual = target.GetStatus(statusId);
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///GetUser のテスト
        ///</summary>
        [TestMethod()]
        public void GetUserTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            User sample = command.Friends(this.getCurrentUserByCommand()).First();
            long userid = sample.UserID;
            target.UpdateUsers(new User[] { sample });

            User actual;
            actual = target.GetUser(userid);
            Assert.IsNotNull(actual);
        }

        /// <summary>
        ///UpdateUserFollowers のテスト
        ///</summary>
        [TestMethod()]
        public void UpdateUserFollowersTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            long userid = this.getCurrentUserByCommand().UserID;
            IEnumerable<User> followers = command.Followers(getCurrentUserByCommand());
            target.UpdateUsers(new User[] { this.getCurrentUserByCommand() });
            target.UpdateUserFollowers(userid, followers);
            target.UpdateUserFollowers(userid, followers);
            Console.WriteLine(target.GetUser(userid).ToString());
        }

        /// <summary>
        ///UpdateDirectMessages のテスト
        ///</summary>
        [TestMethod()]
        public void UpdateDirectMessagesTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            IEnumerable<DirectMessage> directmessages = command.GetSentDirectMessages();
            target.UpdateDirectMessages(directmessages);
            target.UpdateDirectMessages(directmessages);
            Enumerate(directmessages);
        }

        /// <summary>
        ///DestroyDirectMessage のテスト
        ///</summary>
        [TestMethod()]
        public void DestroyDirectMessageTest()
        {
            TwitterCaching target = new TwitterCaching(testConnection);
            IEnumerable<DirectMessage> directmessages = command.GetSentDirectMessages();
            target.UpdateDirectMessages(directmessages);
            long dmid = directmessages.First().DirectMessageID;

            target.DestroyDirectMessage(dmid);
        }
    }
}
